package net.thxjj.netty;

import com.alibaba.fastjson.JSONObject;
import io.netty.channel.ChannelHandlerContext;
import net.model.device.BaseBean;
import net.model.deviceresp.DevRespBean;
import net.model.device.HeartBean;
import net.thxjj.error.ActionType;
import net.thxjj.error.ErrCode;

import java.util.logging.Logger;

public class DeviceHeartHandler extends HeartBeatBaseChannelInBoundHandler {
    private Logger log = Logger.getLogger(String.valueOf(this.getClass()));

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, BaseBean baseBean) throws Exception {

    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        super.channelRead(ctx, msg);
        if(!DeviceSocketMap.hasDevice(ctx.channel())){
            ctx.close();
            return;
        }
        String de = (String)msg;
        HeartBean loginBean =  JSONObject.parseObject(de, HeartBean.class);
        log.info(loginBean.toString());
        DevRespBean devRespBean =  new DevRespBean(loginBean.getMsgId(), ActionType.HEARTBEATRESP.getName(), ErrCode.SUCCESS.getCode(),ErrCode.SUCCESS.getDescription(),null);
        ctx.writeAndFlush(devRespBean.toString());
    }
}
